package com.qh.propertytest;

import android.os.Build;

import java.lang.reflect.Method;

public class PropertyUitls {
    private static  final int PROP_VALUE_MAX = 91;

    public static String getProperty(String key, String defaultValue) {
        String value = defaultValue;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            value = (String)(get.invoke(c, key, defaultValue));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return value;
        }
    }
    public static void setProperty(String key, String val) {

        if (val != null && !val.startsWith("ro.") && val.length() > PROP_VALUE_MAX) {
            throw new IllegalArgumentException("value of system property '" + key
                    + "' is longer than " + PROP_VALUE_MAX + " characters: " + val);
        }
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method set = c.getMethod("set", String.class, String.class);
            set.invoke(c, key, val);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }

    public static String getBuildDeviceListing() {
        return "Build.PRODUCT: " + Build.PRODUCT + "\n" +
                "Build.MANUFACTURER: " + Build.MANUFACTURER + "\n" +
                "Build.BRAND: " + Build.BRAND + "\n" +
                "Build.DEVICE: " + Build.DEVICE + "\n" +
                "Build.MODEL: " + Build.MODEL + "\n" +
                "Build.HARDWARE: " + Build.HARDWARE + "\n" +
                "Build.FINGERPRINT: " + Build.FINGERPRINT + "\n" +
                "Build.TAGS: " + Build.TAGS + "\n";
    }

    public static String getPropertyListing() {
        return "ro.hardware: " + getProperty("ro.hardware", "") + "\n" +
                "ro.product.model: " + getProperty("ro.product.model", "") + "\n" +
                "ro.product.manufacturer: " + getProperty("ro.product.manufacturer", "") + "\n" +
                "ro.product.board: " + getProperty("ro.product.board", "") + "\n" +
                "init.svc.adbd: " + getProperty("init.svc.adbd", "") + "\n" ;
    }

}
